package com.example.li.controller;

import com.example.li.model.Status;
import com.example.li.service.StatusService;
import com.example.li.util.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/status")
public class StatusController {
    @Autowired
    StatusService statusService;

    //查询整个聊天列表
//    @RequestMapping(value = "/getAllStatusList", method = RequestMethod.POST)
//    public ResultDTO getAllStatusList(@RequestBody Status status) {
//        return new ResultDTO().success(statusService.getAllStatusList(status));
//    }
// 查询聊天列表
    @RequestMapping(value = "/getStatusList", method = RequestMethod.POST)
    public ResultDTO getStatusList(@RequestBody Status status) {
        return new ResultDTO().success(statusService.getStatusList(status));
    }

    //添加一条聊天信息
    @RequestMapping(value = "/insertStatus", method = RequestMethod.POST)
    public ResultDTO insertStatus(@RequestBody Status status) {
        return statusService.insertStatus(status);
    }

    //删除一条聊天信息
    @RequestMapping(value = "/deleteStatus", method = RequestMethod.POST)
    public ResultDTO deleteStatus(@RequestBody Status status) {
        return statusService.deleteStatus(status);
    }

    //删除聊天信息列表
    @RequestMapping(value = "/deleteChatList", method = RequestMethod.POST)
    public ResultDTO deleteChatList(@RequestBody Status status) {
        return statusService.deleteChatList(status);
    }

    //用户查询最近聊天人
    @RequestMapping(value = "/getChatList", method = RequestMethod.POST)
    public ResultDTO getChatList(Integer id, HttpServletResponse response, HttpServletRequest request) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            List<Map> lists = statusService.getChatList(id);
            for (int i = 0; i < lists.size(); i++) {
                if (lists.get(i) == null) {
                    lists.remove(i);
                    break;
                }
            }
            resultDTO.setCode("0");
            resultDTO.setData(lists);
            resultDTO.setSuccess(true);
            resultDTO.setMessage("Success");
        } catch (Exception e) {
            resultDTO.setCode("1");
            resultDTO.setData("[]");
            resultDTO.setSuccess(false);
            resultDTO.setMessage("Failure");
            e.printStackTrace();
        }
        return resultDTO;
    }
    //律师查询最近聊天人
    @RequestMapping(value = "/getChatList1", method = RequestMethod.POST)
    public ResultDTO getChatList1(Integer id, HttpServletResponse response, HttpServletRequest request) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            List<Map> lists = statusService.getChatList1(id);
            for (int i1 = 0; i1 < lists.size(); i1++) {
                if (lists.get(i1) == null) {
                    lists.remove(i1);
                    break;
                }
            }
            resultDTO.setCode("0");
            resultDTO.setData(lists);
            resultDTO.setSuccess(true);
            resultDTO.setMessage("Success");
        } catch (Exception e) {
            resultDTO.setCode("1");
            resultDTO.setData("[]");
            resultDTO.setSuccess(false);
            resultDTO.setMessage("Failure");
            e.printStackTrace();
        }
        return resultDTO;
    }

    //查询聊天数目
    @RequestMapping("/getChatCount")
    public Integer getChatCount(Integer id) {
        try {
            return statusService.getChatCount(id);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
